<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="waybillFormRules" :model="waybillForm" label-position="left" label-width="140px">
      <el-form-item label="联系电话" prop="telephone">
        <el-input v-model="waybillForm.telephone" />
      </el-form-item>
      <el-form-item label="收件人" prop="fullname">
        <el-input v-model="waybillForm.fullname" />
      </el-form-item>
      <el-form-item label="区域" prop="area">
        <el-input v-model="waybillForm.area" />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input v-model="waybillForm.address" />
      </el-form-item>
      <el-form-item label="商品信息" prop="code">
        
    <el-table
      key="waybillShopping"
      :data="waybillForm.product"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      >
      <el-table-column label="商品信息" min-width="200">
        <template slot-scope="{row}">
          <el-input v-model="row.productName" />
        </template>
      </el-table-column>
      <el-table-column label="产品编码" min-width="100px">
        <template slot-scope="{row}">
          <el-input v-model="row.productCode" />
        </template>
      </el-table-column>
      <el-table-column label="sku" max-width="150px">
        <template slot-scope="{row}">
          <el-input v-model="row.sku" />
        </template>
      </el-table-column>
      <el-table-column label="数量" max-width="150px">
        <template slot-scope="{row}">
          <el-input v-model="row.quantity" />
        </template>
      </el-table-column>
      <el-table-column label="单价" max-width="150px">
        <template slot-scope="{row}">
          <el-input v-model="row.price" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
      <el-button type="primary" @click="newProduct()">
        新产品
      </el-button>

      </el-form-item>
      <el-form-item label="卖家备注" prop="sellReamrk">
        <el-input v-model="waybillForm.sellReamrk" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="介绍" />
      </el-form-item>
      <el-form-item label="买家备注" prop="buyRemark">
        <el-input v-model="waybillForm.buyRemark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="waybillForm.status.value === 5" type="danger" @click="handleDelete(waybillForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveWaybill, removeWaybillById, getWaybillDetail } from '@/api/logistics/waybill'
import waves from '@/directive/waves' // waves directive
import Upload from '@/components/Upload/SingleImage2' // secondary package based on el-pagination
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { Upload},
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      pickerOptions: {
          disabledDate(time) {
            //return time.getTime() < Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      exchangeOptions:[],
      waybillForm: {
        code: '',
        title: '',
        remark: '',
        product:[
          {
            productName:"",
            procutCode:"",
            quantity:"",
            price:"",
            sku:""
          }
        ],
        status: {
          label: '',
          value: 1
        }
      },
      waybillFormRules: [],
      keywordFormHidden:true,
      keywordForm:{
        title:""
      }
    }
  },
  created() {
    this.loadExchangeList()
    let waybillID = this.$route.query && this.$route.query.waybillID
    if (waybillID !== undefined) {
      waybillID = parseInt(waybillID)
      if (!isNaN(waybillID)) {
        this.listLoading = true
        const formData = {
          'waybillID': waybillID
        }
        getWaybillDetail(formData).then(response => {
          if (response.status === 200) {
            this.waybillForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(waybillID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'waybill' })
    },
    newProduct:function(){
      this.waybillForm.product.push({
            productName:"",
            procutCode:"",
            quantity:"",
            price:"",
            sku:""
      })
    },
    handleKeywordConfirm:function(){
      let keyword = this.keywordForm.title;
      if (keyword) {
        this.waybillForm.keyword.push(keyword);
      }
      this.keywordFormHidden = true;
      this.keywordForm.title = '';
    },
    handleKeywordHidden:function(){
      this.keywordFormHidden = false
      this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleKeywordRemove:function(index){
      this.waybillForm.keyword.splice(index, 1);
    },
    loadExchangeList(){
      
      getExchangeAvabile().then(response => {
        this.exchangeOptions = response.data.list
      })
    },
    createData() {
      this.listLoading = true
      const waybillForm = this.waybillForm
      try {
      waybillForm.status = waybillForm.status.value
      } catch (error) {
      waybillForm.status = 1
      }
      console.log(waybillForm)
      if (waybillForm.ID > 0) {
        waybillForm.waybillID = waybillForm.ID
      }
      waybillForm.product = JSON.stringify(waybillForm.product)
      saveWaybill(waybillForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'waybill' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'waybillID': row.ID,
      }
      removeWaybillById(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'waybill' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
